package com.xl.client;

import javax.websocket.*;

@ClientEndpoint
public class WSClient  {

    @OnOpen
    public void onOpen(Session session) {
        System.out.println("Connected to server");

        // 在连接建立后发送消息到服务器
        try {
            String message = "Hello Server!";
            session.getBasicRemote().sendText(message);

            // 或者通过session对象直接调用sendObject()方法发送自定义类型的数据
            // YourCustomData data = new YourCustomData();
            // session.getBasicRemote().sendObject(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnMessage
    public void onMessage(String message, Session session) {
        System.out.println("Received message from server: " + message);

        // 处理从服务器收到的消息
    }

    @OnClose
    public void onClose(Session session, CloseReason closeReason) {
        System.out.println("Connection closed by server with reason: " + closeReason.getReasonPhrase());
    }


}
